package cn.qn.service.impl.news;

import cn.qn.base.PageVO;
import cn.qn.dto.news.AlbumDTO;
import cn.qn.dto.news.AlbumQueryDTO;
import cn.qn.mapper.news.AlbumMapper;
import cn.qn.moudle.image.Image;
import cn.qn.moudle.news.Album;
import cn.qn.repository.image.ImageRepository;
import cn.qn.repository.news.AlbumRepository;
import cn.qn.service.news.AlbumService;
import cn.qn.util.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Jeet
 * @description
 * @date 2019/5/30 11:28
 */

@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = AlbumService.class)
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumRepository albumRepository;
    @Resource
    private AlbumMapper albumMapper;
    @Autowired
    private ImageRepository imageRepository;

    @Override
    public Album insert(AlbumDTO album) {
        Album a = new Album();
        a.setCover(album.getCover());
        a.setBanner(album.getBanner());
        a.setName(album.getName());
        a.setRemark(album.getRemark());
        a.setStatus(album.getStatus());
        a.setCreateBy(album.getCreateBy());
        a.setUpdateBy(album.getUpdateBy());
        StringBuilder sb = new StringBuilder();
        for (Image im:album.getPhotos()) {
            sb.append(im.getId()).append(",");
        }
        String s = sb.toString();
        a.setAlbum(s.substring(0,s.length()-1));
        return albumRepository.save(a);
    }

    @Override
    public PageVO<AlbumDTO> page(AlbumQueryDTO baseQuery) {
        PageBounds pb = baseQuery.getPageBounds();
        List<AlbumDTO> list = albumMapper.page(pb,baseQuery);
        Integer pageCount = albumMapper.pageCount(baseQuery);
        return PageVO.convert(pb.getPage(),pb.getLimit(),list,pageCount);
    }

    @Override
    public AlbumDTO findById(Integer id) {
        Album album = albumRepository.findById(id).get();
        AlbumDTO dto = new AlbumDTO();
        BeanUtils.copyProperties(album,dto);
        String[] ids = album.getAlbum().split(",");
        dto.setCreateTime(DateUtils.parseDateToStr(album.getCreateTime(),DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        List<Image> path = new ArrayList<>();
        Arrays.asList(ids).stream().forEach(i->{
            path.add(imageRepository.findImageById(Integer.valueOf(i)));
        });
        dto.setAlbumPath(path);
        dto.setCoverPath(imageRepository.findImageById(album.getCover()).getPath());
        dto.setBannerPath(imageRepository.findImageById(album.getBanner()).getPath());
        return dto;
    }

    @Override
    public Album update(AlbumDTO album) {
        Album a = albumRepository.findById(album.getId()).get();
        a.setCover(album.getCover());
        a.setBanner(album.getBanner());
        a.setName(album.getName());
        a.setRemark(album.getRemark());
        a.setStatus(album.getStatus());
        a.setUpdateBy(album.getUpdateBy());
        StringBuilder sb = new StringBuilder();
        for (Image im:album.getPhotos()) {
            sb.append(im.getId()).append(",");
        }
        String s = sb.toString();
        a.setAlbum(s.substring(0,s.length()-1));
        return albumRepository.save(a);
    }
}
